package com.smalls.newvideotwo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smalls.newvideotwo.R;

import java.util.ArrayList;
import java.util.List;

public class NewIndexAdapter extends RecyclerView.Adapter<NewIndexAdapter.ViewHolder> {
    private Context context;
    private List<String> lists;
    private int selcted = -1;
    private TextView selectView = null;
    private View.OnClickListener mOnClickListener = null;
    private View.OnFocusChangeListener mOnFocusListener = null;

    public NewIndexAdapter(Context paramContext, List<String> paramArrayList) {
        this.context = paramContext;
        if (paramArrayList != null) {
            this.lists = paramArrayList;
        } else {
            this.lists = new ArrayList();
        }
    }

    @Override
    public NewIndexAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View localView = LayoutInflater.from(this.context).inflate(R.layout.new_home_index_item,viewGroup, false);
        return new ViewHolder(localView);
    }

    @Override
    public void onBindViewHolder(final NewIndexAdapter.ViewHolder viewHolder, final int i) {
        viewHolder.itemView.setTag(i);
        viewHolder.localTextView.setText((lists.get(i)));
        viewHolder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i("NewIndexAdapter","onFocusChange hasFocus="+hasFocus+" v="+v);
                if(hasFocus) {
                    viewHolder.itemShadow.setVisibility(View.VISIBLE);
                    viewHolder.localTextView.setTextColor(context.getResources().getColor(R.color.setting_text_select));
                } else {
                    viewHolder.itemShadow.setVisibility(View.GONE);
                    if(selcted!=i) {
                        viewHolder.localTextView.setTextColor(context.getResources().getColor(R.color.search_right_bg));
                    }
                }

                if(mOnFocusListener!=null) {
                    mOnFocusListener.onFocusChange(v,hasFocus);
                }
            }
        });

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("NewIndexAdapter","onClick v="+v);
//                selcted = i;
//                if(selectView!=null) {
//                    selectView.setTextColor(context.getResources().getColor(R.color.search_right_bg));
//                    selectView = null;
//                }
//                viewHolder.localTextView.setTextColor(context.getResources().getColor(R.color.setting_text_select));
                selectView = viewHolder.localTextView;
                if(mOnClickListener!=null) {
                    mOnClickListener.onClick(v);
                }
            }
        });
        viewHolder.itemView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if(i==0
                        &&keyCode==KeyEvent.KEYCODE_DPAD_LEFT
                        &&keyEvent.getAction()==KeyEvent.ACTION_DOWN) {
                    return true;
                }
                return false;
            }
        });
    }

    public void setOnFocusListener(View.OnFocusChangeListener listener) {
        mOnFocusListener = listener;
    }

    public void setOnClickListener(View.OnClickListener listener) {
        mOnClickListener = listener;
    }

    public long getItemId(int paramInt) {
        return (long) paramInt;
    }

    @Override
    public int getItemCount() {
        if(lists!=null) {
            return lists.size();
        }
        return 0;
    }

    public int getSelectItem() {
        return selcted;
    }

    public void setSelctItem(int paramInt) {
        this.selcted = paramInt;
        notifyDataSetChanged();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView localTextView;
        ImageView itemShadow;
        public ViewHolder(View itemView) {
            super(itemView);
            localTextView = (TextView) itemView.findViewById(R.id.index_name);
            itemShadow = (ImageView)itemView.findViewById(R.id.index_shadow);
        }
    }
}
